package com.hmsg.health.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 响应对象
 *
 * @author Lujq
 * @since 1.0.0
 */
public class ApiResponse {
	public static Logger logger = LoggerFactory.getLogger(ApiResponse.class);

	private String errorCode;
	private String message;
	private Object data;

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	@SuppressWarnings("unchecked")
	@JsonIgnore
	public Map<String, Object> getMapData() {
		try {
			return (Map<String, Object>) data;
		} catch (Exception e) {
			logger.error("getMapData erroe ", e);
			return new HashMap<>();
		}
	}

	@SuppressWarnings("unchecked")
	@JsonIgnore
	public List<Object> getListData() {
		try {
			return (List<Object>) data;
		} catch (Exception e) {
			logger.error("getListData erroe ", e);
			return new ArrayList<>();
		}
	}

	public void setData(Object data) {
		this.data = data;
	}

	public static ApiResponse success(Object data) {
		ApiResponse response = success();
		response.setData(data);
		return response;
	}
	
	@SuppressWarnings("unchecked")
	public static ApiResponse successPage(@SuppressWarnings("rawtypes") Page pageData) {
		ApiResponse response = success();
		PagingResult<Object> result = new PagingResult<>();
		result.setLast(pageData.isLast());
		result.setNumber(pageData.getNumber());
		result.setNumberOfElements(pageData.getNumberOfElements());
		result.setSize(pageData.getSize());
		result.setTotalElements((int) pageData.getTotalElements());
		result.setTotalPages(pageData.getTotalPages());
		result.setContent(pageData.getContent());
		response.setData(result);
		return response;
	}

	public static ApiResponse success() {
		ApiResponse response = new ApiResponse();
		response.setErrorCode(ErrorCode.SUCCESS.value());
		response.setMessage(ErrorCode.SUCCESS.memo());
		return response;
	}

	public static ApiResponse fail(ErrorCode errorCode) {
		ApiResponse data = new ApiResponse();
		data.setErrorCode(errorCode.value());
		data.setMessage(errorCode.memo());
		return data;
	}

	public static ApiResponse fail(ErrorCode errorCode, String msg) {
		ApiResponse data = new ApiResponse();
		data.setErrorCode(errorCode.value());
		data.setMessage(msg);
		return data;
	}

	public static ApiResponse defaultFail() {
		ApiResponse data = new ApiResponse();
		data.setErrorCode(ErrorCode.FAIL.value());
		data.setMessage(ErrorCode.FAIL.memo());
		return data;
	}

	@Override
	public String toString() {
		return JsonUtil.toJson(this);
	}
	
	@JsonIgnore
	public boolean isSuccess() {
		if (ErrorCode.SUCCESS.value().equals(this.errorCode)) {
			return true;
		}
		throw new ApiResponseException(this);
	}

}
